package com.epam.cdp.jpa.converters;

import java.util.Arrays;
import java.util.Collection;

import javax.ejb.Stateless;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import com.epam.cdp.jpa.model.BookCategory;

@Stateless
public class BookCategoriesToTreeNodeMapper {

    public TreeNode toTreeNode(BookCategory ... bookCategories) {
	return toTreeNode(Arrays.asList(bookCategories));
    }
    
    public TreeNode toTreeNode(Collection<BookCategory> bookCategories) {
	TreeNode parentNode = new DefaultTreeNode();
	addChildren(parentNode, bookCategories);
	return parentNode;
//	return toTreeNode(bookCategories.toArray(new BookCategory[0]));
    }
    
//    public BookCategory toBookCategory(TreeNode treeNode) {
//	
//    }
    
    private void addChildren(TreeNode parentNode, Collection<BookCategory> bookCategories) {
	if (bookCategories != null) {
	    for (BookCategory category : bookCategories) {
		TreeNode currentNode = new DefaultTreeNode(category, parentNode);
		addChildren(currentNode, category.getChildren());
	    }
	}
    }
}
